/**
 * goio-bean
 * 
 * This class do
 * 
 * @link http://www.marcosperon.com/
 * @copyright 2009 - Peron Co.
 * @author Marcos Peron
 * @package com.marcosperon.dao.creator
 * @version 2009
 */
package com.mperon.dao.creator;

import com.mperon.dao.DAO;
import com.mperon.dao.DAOCreator;
import com.mperon.util.java.ReflectUtils;

/**
 * The Class SimpleDAOCreator.
 * 
 * @author marcos
 */
public class SimpleDAOCreator implements DAOCreator {

    /** The class prefix. */
    private String classPrefix;

    /** The class suffix. */
    private String classSuffix;

    /** The package name. */
    private String packageName;

    /**
     * Instantiates a new simple dao creator.
     */
    public SimpleDAOCreator() {
    }

    /**
     * The Constructor.
     * 
     * @param cPrefix
     *            the c prefix
     * @param cSuffix
     *            the c suffix
     * @param pName
     *            the name
     */
    public SimpleDAOCreator(String cPrefix, String cSuffix, String pName) {
        setClassPrefix(cPrefix);
        setClassSuffix(cSuffix);
        setPackageName(pName);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.marcosperon.dao.DAOCreator#getDAO(java.lang.Class)
     */
    @Override
    public DAO<?> getDAO(Class<?> clz) {
        String className = buildDAOClassName(clz);
        return (DAO<?>)ReflectUtils.newInstance(className);
    }

    /**
     * Builds the dao class name.
     * 
     * @param clz
     *            the clz
     * 
     * @return the string
     */
    public String buildDAOClassName(Class<?> clz) {
        StringBuilder sb = new StringBuilder();
        sb.append(clz.getPackage().getName());
        if (!"".equals(packageName)) {
            sb.append('.');
        }
        sb.append(packageName);
        if (!"".equals(packageName)) {
            sb.append('.');
        }
        sb.append(classPrefix);
        sb.append(clz.getSimpleName());
        sb.append(classSuffix);
        return sb.toString();
    }

    /**
     * Sets the class prefix.
     * 
     * @param cPrefix
     *            the new class prefix
     */
    public void setClassPrefix(String cPrefix) {
        classPrefix = cPrefix == null ? "" : cPrefix;
    }

    /**
     * Sets the class suffix.
     * 
     * @param cSuffix
     *            the new class suffix
     */
    public void setClassSuffix(String cSuffix) {
        classSuffix = cSuffix == null ? "" : cSuffix;
    }

    /**
     * Sets the package name.
     * 
     * @param pName
     *            the new package name
     */
    public void setPackageName(String pName) {
        packageName = pName == null ? "" : pName;
    }

}
